const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const bodyParser = require("body-parser");
const cors =require("cors");


const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 自定义中间件，用于统一返回结果
app.use((req, res, next) => {
  res.sendSuccess = (data,message,statusCode=200) => {
    res.status(200).json({
      code:statusCode,
      success: true,
      message,
      data
    });
  };

  res.sendError = (errorMessage,statusCode=400) => {
    res.status(200).json({
      code:statusCode,
      success: false,
     data:null,
      message: errorMessage
    });
  };

  next();
});
app.get("/", (req, res, next) => {
  res.json({"status":"Alive"})
});

require("./app/routes/users.route")(app);
require("./app/routes/captcha.route")(app);
module.exports = app;
